﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using METU_CMT.Business.Compression;
using METU_CMT.Business.Report;

namespace METU_CMT.Business
{
    public class Instructor : User
    {
        //private List<Course> assignedCourses;

        public Instructor(string userName)
        {
            UsersTableAdapter _usersTableAdapter = new UsersTableAdapter();
            //assignedCourses = _usersTableAdapter.GetAssignedCourses( _usersTableAdapter.GetUserId( userName ) );
            UserId = _usersTableAdapter.GetUserId(userName);
            UserName = userName;
            Name = _usersTableAdapter.GetName(UserId);
            Surname = _usersTableAdapter.GetSurname(UserId);
            Email = _usersTableAdapter.GetEMail(UserId);
            PhotoFile = _usersTableAdapter.GetPhotoFilePath(UserId);

        }

        public Instructor(int userId)
        {
            UsersTableAdapter _usersTableAdapter = new UsersTableAdapter();
            UserName = _usersTableAdapter.GetUserName(userId);
            //assignedCourses = _usersTableAdapter.GetAssignedCourses(userId);
            UserId = userId;
            Name = _usersTableAdapter.GetName(UserId);
            Surname = _usersTableAdapter.GetSurname(UserId);
            Email = _usersTableAdapter.GetEMail(UserId);
            PhotoFile = _usersTableAdapter.GetPhotoFilePath(UserId);

        }

        public Instructor()
        {
            //assignedCourses = new List< Course >();
        }

        public override void GoToProfilePage(HttpResponse response)
        {
            response.Redirect("~/InstructorMainScreen.aspx");
        }

        public override void SetHome(MenuItem menuItem)
        {
            menuItem.NavigateUrl = "~/InstructorMainScreen.aspx";
        }

        public void DownloadDocuments()
        {
            CompressionAdapter _compressionAdapter = new CompressionAdapter();
            CourseInstructor _courseInstructor = (CourseInstructor)EntityFactory.GetCurrentCourse(UserType.Instructor);


            foreach (Team _team in _courseInstructor.Teams)
            {
                foreach (Event _event in _courseInstructor.Schedule)
                {
                    Deliverable _d = _team.Deliverables.FindLast(deliverable => deliverable.Event.EventId == _event.EventId);
                    if (_d != null)
                    {
                        _compressionAdapter.AddFile(_d, _team.TeamName );
                    }
                }
            }
            _compressionAdapter.CompressAndDownloadDeliverables();
        }

        public void AddNewUser(string name, string surname, string username, string password, string email, UserType role, DateTime expiryDate)
        {
            User _newUser;

            switch (role)
            {
                case UserType.Instructor:
                    _newUser = new Instructor();
                    break;
                case UserType.Student:
                    _newUser = new Student();
                    ((Student)_newUser).ExpiryDate = expiryDate;
                    break;
                default:
                    _newUser = new TeachingAssistant();
                    break;
            }

            _newUser.Name = name;
            _newUser.Surname = surname;
            _newUser.UserName = username;
            _newUser.PassWord = password;
            _newUser.Email = email;

            UsersTableAdapter _usersTableAdapter = new UsersTableAdapter();
            _usersTableAdapter.InsertUser(_newUser, role);
        }

        public void AssignTeams(ReviewTeam reviewTeam)
        {
            CourseInstructor _c = (CourseInstructor)EntityFactory.GetCurrentCourse(UserType.Instructor);
            _c.AddReviewer(reviewTeam);
        }

        public void DeleteCourse(Course course)
        {
            course.Instructors.RemoveAll(instructor => instructor.UserId == EntityFactory.User.UserId);

            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.RemoveCourse(course);
            _courseTableAdapter.RemoveCourseInstructor(course, UserId);
        }

        public void UpdateCourse(CourseInstructor newCourse)
        {
            UsersTableAdapter _usersTableAdapter = new UsersTableAdapter();
            _usersTableAdapter.UpdateCourse(newCourse);
        }

        public void AssignStudentToCourse(int courseId, int studentId)
        {
            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.InsertStudent(courseId, studentId);
        }

        public void UpdateUser(int studentId, string name, string surname, string username, string email, DateTime expiryDate)
        {
            try
            {
                DataSets.METUCMTDataSetTableAdapters.UsersTableAdapter _usersTableAdapter = new DataSets.METUCMTDataSetTableAdapters.UsersTableAdapter();
                _usersTableAdapter.UpdateUser(username, name, surname, email, expiryDate, studentId);
            }
            catch (SqlException)
            {
            }
        }

        public void DeleteStudentFromCourse(int courseId, int studentId)
        {
            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.RemoveCourseStudent(courseId, studentId);
        }

        public void AssignTeachingAssistantToCourse(int courseId, int teachingAssistantId)
        {
            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.InsertTA(courseId, teachingAssistantId);
        }

        public void DeleteTeachingAssistantFromCourse(int courseId, int teachingAssistantId)
        {
            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.RemoveTa(courseId, teachingAssistantId);
        }

        public void DownloadSubmissionReport()
        {
            ReportGenerator _reportGenerator = new ReportGenerator();
            CourseInstructor _courseInstructor = (CourseInstructor)EntityFactory.GetCurrentCourse(UserType.Instructor);

            foreach (Team _team in _courseInstructor.Teams)
            {
                foreach (Event _event in _courseInstructor.Schedule)
                {
                    Deliverable _d = _team.Deliverables.FindLast(deliverable => deliverable.Event.EventId == _event.EventId);
                    if(_d == null)
                    {
                        _d = new Deliverable {Event = _event};
                    }
                    ReportInfo _reportInfo = new ReportInfo(_team, _d);
                    _reportGenerator.AddReportInfo(_reportInfo);
                }
            }
            _reportGenerator.CreateReportAndDownload();
        }
    }
}